﻿using UnityEngine;
using System.Collections;
using spaar.ModLoader;
using spaar.ModLoader.UI;
using LiftIndicatorMOD;
using UnityEngine.SceneManagement;

public class SettingWindow : MonoBehaviour
{

    public int WindowID { get; } = Util.GetWindowID();

    public string WindowName = "升力指示器设置";

    public Rect WindowRect = new Rect(500, 500, 240, 128);

    /// <summary>
    /// 模块升力可视
    /// </summary>
    public static bool liftVisiabled = true;

    /// <summary>
    /// 指示器升力可视
    /// </summary>
    public static bool liftIndicatorVisiabled = true;

    /// <summary>
    /// 模拟飞行速度
    /// </summary>
    public static Vector3 simulateVelocity_Vector = Vector3.zero;

    public LineRenderer simulateVelocity_Renderer;

    private void Start()
    {

        if (GetComponent<LineRenderer>() == null)
        {
            simulateVelocity_Renderer = gameObject.AddComponent<LineRenderer>();
        }
        else
        {
            simulateVelocity_Renderer = GetComponent<LineRenderer>();
        }
        simulateVelocity_Renderer.useWorldSpace = true;
        simulateVelocity_Renderer.SetVertexCount(2);
        simulateVelocity_Renderer.material = new Material(Shader.Find("Particles/Additive"));
        simulateVelocity_Renderer.SetColors(Color.white, Color.cyan);
        simulateVelocity_Renderer.SetWidth(0.5f, 0.5f);
    }

    private void Update()
    {
        if (!StatMaster.isSimulating)
        {
            simulateVelocity_Renderer.SetPosition(0, Vector3.zero);
            simulateVelocity_Renderer.SetPosition(1, simulateVelocity_Vector);
        }
    }

    void OnGUI()
    {
        if (StatMaster.isSimulating || StatMaster.inMenu || LiftBlockScript.liftBlocks.Count < 1 || !LiftIndicatorMod.ModEnabled)
        {
            simulateVelocity_Renderer.enabled = false;
            return;
        }

        GUI.skin = Skin;
        WindowRect = GUI.Window(WindowID, WindowRect, DoWindow, WindowName, Elements.Windows.ClearDark);
        simulateVelocity_Renderer.enabled = true;
    }

    void DoWindow(int id)
    {
        GUI.Label(new Rect(27,24,128,32), string.Format("模拟速度\n    X\t      Y\t         Z\n  {0}\t     {1}\t        {2}", simulateVelocity_Vector.x, simulateVelocity_Vector.y, simulateVelocity_Vector.z));

        //X+
        if (GUI.Button(new Rect(16, 56, 12, 12), "+"))
        {
            simulateVelocity_Vector.x += 10f;
        }
        //x-
        if (GUI.Button(new Rect(64, 56, 12, 12), "-"))
        {
            simulateVelocity_Vector.x -= 10f;
        }
        
        //y+
        if (GUI.Button(new Rect(92, 56, 12, 12), "+"))
        {
            simulateVelocity_Vector.y += 10f;
        }
        //y-
        if (GUI.Button(new Rect(140, 56, 12, 12), "-"))
        {
            simulateVelocity_Vector.y -= 10f;
        }

        //z+
        if (GUI.Button(new Rect(168, 56, 12, 12), "+"))
        {
            simulateVelocity_Vector.z += 10f;
        }
        //z-
        if (GUI.Button(new Rect(212, 56, 12, 12), "-"))
        {
            simulateVelocity_Vector.z -= 10f;
        }


        if (GUI.Button(new Rect(16,80,96,32), "升力可视", liftVisiabled ? Elements.Buttons.Default: Elements.Buttons.Red))
        {
            liftVisiabled = !liftVisiabled;
        }

        if (GUI.Button(new Rect(128, 80, 96, 32), "升心可视", liftIndicatorVisiabled ? Elements.Buttons.Default : Elements.Buttons.Red))
        {
            liftIndicatorVisiabled = !liftIndicatorVisiabled;
        }

        GUI.DragWindow(new Rect(0, 0, WindowRect.width, WindowRect.height));
    }


    /// <summary>
    /// Unity IMGUI transparent.
    /// </summary>
    public static GUISkin Skin
    {
        get
        {
            var skin = ModGUI.Skin;
            GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f, 0.7f);
            skin.window.padding.left = 8;
            skin.window.padding.right = 8;
            skin.window.padding.bottom = 8;
            return skin;
        }
    }

}
